package com.haoshuang.zhouzhoubang.ui.page.order.scan

import android.content.Context
import android.os.Bundle
import android.os.Vibrator
import android.util.Log
import cn.bingoogolapple.qrcode.core.QRCodeView
import com.gyf.immersionbar.ktx.immersionBar
import com.haoshuang.zhouzhoubang.databinding.ActivityScanBinding
import com.haoshuang.zhouzhoubang.ui.page.order.scan.inputcode.InputCodeActivity
import com.haoshuang.zhouzhoubang.ui.page.order.scan.writeofforder.WriteOffOrderActivity
import com.haoshuang.zhouzhoubang.ui.page.order.scan.writeoffresult.WriteOffResultActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 * 扫一扫
 */
class ScanActivity : BaseVMActivity<ScanViewModel, ActivityScanBinding>(), QRCodeView.Delegate {


    val TAG: String =
        ScanActivity::class.java.getSimpleName()

    companion object {

        fun launch(
            context: Context
        ) {
            context.startPage<ScanActivity>()
        }
    }

    override fun initImmersionBar() {
        immersionBar {
            transparentStatusBar()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding?.zxingview?.setDelegate(this);
        binding?.tvSrqm?.clickWithTrigger {
            InputCodeActivity.launch(this)
            finish()
        }
    }

    override fun initData() {

    }

    override fun onStart() {
        super.onStart()

        binding?.zxingview?.startCamera() // 打开后置摄像头开始预览，但是并未开始识别

        //        mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别
        binding?.zxingview?.startSpotAndShowRect() // 显示扫描框，并开始识别
    }

    override fun onStop() {
        binding?.zxingview?.stopCamera() // 关闭摄像头预览，并且隐藏扫描框
        super.onStop()
    }

    override fun onDestroy() {
        binding?.zxingview?.onDestroy() // 销毁二维码扫描控件
        super.onDestroy()
    }

    override fun onScanQRCodeSuccess(result: String) {
        Log.i(TAG, "scanresult:$result")
        vibrate()
        vm.getHexiaoOrderInfo(result)
        binding?.zxingview?.stopSpot()
    }




    override fun startObserve() {
        vm.getHexiaoOrderInfoData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                WriteOffResultActivity.launch(this@ScanActivity,false,msg)
                finish()
            }
            onSuccess {
                dismissLoading()
                if (it?.data != null) {
                    WriteOffOrderActivity.launch(this@ScanActivity, it.data!!)
                    finish()
                }

            }
        }


    }


    private fun vibrate() {
        val vibrator = getSystemService(VIBRATOR_SERVICE) as Vibrator
        vibrator.vibrate(200)
    }


    override fun onCameraAmbientBrightnessChanged(isDark: Boolean) {


        // 这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
       /* var tipText: String = binding?.zxingview?.getScanBoxView()!!.getTipText()
        val ambientBrightnessTip = "\n环境过暗，请打开闪光灯"
        if (isDark) {
            if (!tipText.contains(ambientBrightnessTip)) {
                binding?.zxingview?.getScanBoxView()!!.setTipText(tipText + ambientBrightnessTip)
            }
        } else {
            if (tipText.contains(ambientBrightnessTip)) {
                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip))
                binding?.zxingview?.getScanBoxView()!!.setTipText(tipText)
            }
        }*/
    }

    override fun onScanQRCodeOpenCameraError() {
        Log.e( TAG, "打开相机出错")
    }
}